package org.lalib.controller.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.lalib.controller.form.CItemEditForm;
import org.lalib.controller.service.CCategoryService;
import org.lalib.controller.service.CFoodService;
import org.lalib.controller.service.CHistoricalValueService;
import org.lalib.controller.service.CTableService;
import org.lalib.controller.service.CUserService;
import org.lalib.controller.service.CVariableService;
import org.lalib.entity.object.EFood;
import org.lalib.entity.object.EHistoricalValue;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.ETable;
import org.lalib.entity.object.EUser;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class CAbstractItemOperationAction {

	@Autowired
	protected CItemEditForm cItemEditForm;

	@Autowired
	protected CUserService cUserService;

	@Autowired
	protected CTableService cTableService;

	@Autowired
	protected CFoodService cFoodService;

	@Autowired
	protected CVariableService cVariableService;
	
	@Autowired
	protected CCategoryService cCategoryService;

	@Autowired
	protected CHistoricalValueService cHistoricalValueService;

	public abstract String init(EItem item);

	public abstract String save();

	public abstract String cancel();

	@SuppressWarnings("rawtypes")
	public final List<EHistoricalValue> getValues(Object tabname) {
		String tabString = tabname.toString();
		if (cItemEditForm.getHistoricalValueMap().containsKey(tabString))
			return cItemEditForm.getHistoricalValueMap().get(tabString);
		List<EHistoricalValue> historicalValues = cHistoricalValueService.getHistoricalValuesForTabName(cItemEditForm.getItem(), tabString);
		cItemEditForm.getHistoricalValueMap().put(tabString, historicalValues);
		return historicalValues;
	}

	protected final EItem getRefreshedItem(EItem item) {
		if (item instanceof EUser)
			item = cUserService.getUserById(item.getId());
		else if (item instanceof EFood)
			item = cFoodService.getFoodById(item.getId());
		else
			item = cTableService.getTableById(item.getId());
		return item;
	}

	protected final void save(EItem item) {
		if (item instanceof EUser)
			cUserService.update((EUser) item);
		else if (item instanceof EFood)
			cFoodService.update((EFood) item);
		else if (item instanceof ETable)
			cTableService.update((ETable) item);
	}

	@SuppressWarnings("rawtypes")
	public final void generateHistoricalValues() {
		Object value = cItemEditForm.getValue();
		if (value != null) {
			List<EHistoricalValue> values = cHistoricalValueService.getHistoricalValues((EHistoricalValue) value);
			cItemEditForm.setSelectedValues(values);
		}
	}

	@SuppressWarnings("rawtypes")
	protected final List<EHistoricalValue> getValuesFromMap(Map<String, List<EHistoricalValue>> historicalValueMap) {
		List<EHistoricalValue> eHistoricalValues = new ArrayList<>();
		for (List<EHistoricalValue> historicalValues : historicalValueMap.values())
			eHistoricalValues.addAll(historicalValues);
		return eHistoricalValues;
	}
}